package cn.edu.hitsz.task;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Category;

import populus.core.queue.URLLinkedBlockingQueue;
import cn.edu.hitsz.backup.UncommitedQueue;
import cn.edu.hitsz.core.Connection;
import cn.edu.hitsz.http.HTTPRequest;
import cn.edu.hitsz.http.HTTPResponse;
import cn.edu.hitsz.infra.DefaultLinkExtract;

/**
 * A CAPSULATION OF HTTPREQUEST AND CONNECTION
 * @author mingchen
 *
 */
public class FetchTask implements Runnable{
	private final Category log= Category.getInstance(HTTPRequest.class);
	private HTTPRequest request;
	private Connection conn;
	/**
	 * Constructor function of the class
	 */
	public FetchTask(){

	}
	public FetchTask(HTTPRequest request,Connection conn){
		this.request = request;
		this.conn = conn;
	}
	/**
	 * Implementation of the interface method
	 */
	public void run() {
		HTTPResponse response = null;
		try{
			response = request.GET();
			DefaultLinkExtract dle = new DefaultLinkExtract(response.getContent(),request.getUrl());
			// the following to operations should be atom

			if (URLLinkedBlockingQueue.linkqueue.addAll(dle.offerLinks())) {
				if (conn.offerData(trimData(response))) {
					UncommitedQueue.uncommited.remove(request.getUrl());
				} else {
					log.error("Data pipeline error! Cannot not been stored"
							+ request.getUrl().getUrl());
				}
			} else {
				URLLinkedBlockingQueue.linkqueue.add(request.getUrl());
			}



		}catch(Exception e){
			e.printStackTrace();
		}
		log.info("Fetch ["+request.getUrl().toString()+"],["+response.getContent().length()+"]");
	}

	/**
	 * Map the response into HashMap key-value mode
	 * 
	 * @param response
	 * @return Map<String,String> 2015年8月17日下午3:05:22
	 */
	public Map<String,String> trimData(HTTPResponse response){
		Map<String,String> map = new HashMap<String,String>();
		map.put(response.getURLs(), response.getContent());
		return map;
	}


}
